### Project 29: Intrusion Detection ![](media/b7828b9e5ee615a151567e20d35db90f.png) **Description** In this experiment, we use a PIR motion sensor to control an active buzzer to emit sounds and the onboard LED to flash rapidly. **Required Components** ![image-20231020085335159](media/image-20231020085335159.png) **Connection Diagram** ![](media/07ded8ae2b9b12d7d399422cae6b8c5a.png) **Test Code** ```c //********************************************************************************** /* * Filename : PIR alarm * Description : PIR control buzzer * Auther : http//www.keyestudio.com */ int item = 0; void setup() { pinMode(15, INPUT); //PIR motion sensor is connected to GPIO15 and set as the input mode pinMode(4, OUTPUT);//The active buzzer is connected to GPIO4 and set to output mode pinMode(22, OUTPUT);//LED is connected to GPIO22 and set to output mode } void loop() { item = digitalRead(15);//Read digital level signal output by infrared pyrorelease sensor if (item == 1) { //Movement detected digitalWrite(4, HIGH); //Turn on the buzzer digitalWrite(22, HIGH); //Turn on the LED delay(200);//Delay 200ms digitalWrite(4, LOW); //Turn off the buzzer digitalWrite(22, LOW); //Turn off the LED delay(200);//Delay 200ms } else { //No detection digitalWrite(4, LOW); //Turn off the buzzer digitalWrite(22, LOW); //Turn off the LED } } //********************************************************************************** ``` **Test Result** Connect the wires according to the experimental wiring diagram, compile and upload the code to the ESP32. After uploading successfully,we will use a USB cable to power on. If the sensor detects people moving, the buzzer will emit an alarm , and the LED will flash continuously.